import { _decorator, Node, UITransform, Vec2 } from 'cc';
import { EntityManager } from '../../Base/EntityManager';
import { IActor, InputTypeEnum, IWeaponShoot } from '../../Common';
import { toFixed } from '../../Common/Utils';
import { EntityStateEnum, EventEnum } from '../../Enum';
import DataManager from '../../Global/DataManager';
import EventManager from '../../Global/EventManager';
import { WeaponStateMachine } from './WeaponStateMachine';
const { ccclass } = _decorator;

@ccclass("WeaponManager")
export class WeaponManager extends EntityManager {

    public owner: number;

    private body: Node = null;
    private anchor: Node = null;
    private point: Node = null;

    init(data: IActor) {
        this.owner = data.id;

        this.body = this.node.getChildByName("Body");
        this.anchor = this.body.getChildByName("Anchor");
        this.point = this.anchor.getChildByName("Point");

        this.fsm = this.body.addComponent(WeaponStateMachine);
        this.fsm.init(data.weaponTpye)

        this.state = EntityStateEnum.Idle;
        this.initEvents();
    }

    initEvents() {
        EventManager.Instance.on(EventEnum.WeaponShoot, this.handleWeaponShoot, this);
        EventManager.Instance.on(EventEnum.BulletBorn, this.handleBulletBorn, this);
    }
    protected onDestroy(): void {
        EventManager.Instance.off(EventEnum.WeaponShoot, this.handleWeaponShoot, this);
        EventManager.Instance.off(EventEnum.BulletBorn, this.handleBulletBorn, this);
    }

    handleBulletBorn(owenr: number) {
        if (owenr !== this.owner) {
            return;
        }

        this.state = EntityStateEnum.Attack;
    }

    handleWeaponShoot() {
        if (this.owner !== DataManager.Instance.myPlayerId) {
            return;
        }
        const pointWorldPos = this.point.getWorldPosition();
        const pointStagePos = DataManager.Instance.stage.getComponent(UITransform).convertToNodeSpaceAR(pointWorldPos);
        const anchorWorldPos = this.anchor.getWorldPosition();

        const direction = new Vec2(pointWorldPos.x - anchorWorldPos.x, pointWorldPos.y - anchorWorldPos.y).normalize();

        const input: IWeaponShoot = {
            owner: this.owner,
            type: InputTypeEnum.WeaponShoot,
            position: {
                x: toFixed(pointStagePos.x),
                y: toFixed(pointStagePos.y),
            },
            direction: {
                x: toFixed(direction.x),
                y: toFixed(direction.y),
            },
        }
        EventManager.Instance.emit(EventEnum.ClientSync, input)
        // DataManager.Instance.applyInput({
        //     owner: this.owner,
        //     type: InputTypeEnum.WeaponShoot,
        //     position: {
        //         x: pointStagePos.x,
        //         y: pointStagePos.y,
        //     },
        //     direction: {
        //         x: direction.x,
        //         y: direction.y,
        //     },
        // })
        // console.log(DataManager.Instance.state.bullets)
    }
}